package cn.itsource.order.service.impl;

import cn.itsource.basic.config.BussinessException;
import cn.itsource.order.domain.OrderCancleLog;
import cn.itsource.order.domain.OrderProduct;
import cn.itsource.order.dto.OrderConfirmDto;
import cn.itsource.order.mapper.OrderProductMapper;
import cn.itsource.order.service.IOrderCancleLogService;
import cn.itsource.order.service.IOrderProductService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.product.domain.Product;
import cn.itsource.product.domain.ProductTohandle;
import cn.itsource.product.service.IProductService;
import cn.itsource.product.service.IProductTohandleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-11-03
 */
@Service
public class OrderProductServiceImpl extends BaseServiceImpl<OrderProduct> implements IOrderProductService {
    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private IProductTohandleService productTohandleService;
    @Autowired
    private IProductService productService;
    @Autowired
    private IOrderCancleLogService orderCancleLogService;

    @Override
    public void confirm(OrderConfirmDto orderConfirmDto) {
        // 1.校验订单是否存在
        OrderProduct orderProduct = orderProductMapper.findById(orderConfirmDto.getId());
        if (orderProduct == null) {
            throw new BussinessException("此订单不存在来了");
        }
        // 2.查询寻主信息修改状态为已完成
        ProductTohandle productTohandle = productTohandleService.findById(orderProduct.getProductId());
        if (productTohandle == null) {
            throw new BussinessException("此寻主消息已不存在");
        }
        productTohandle.setState(3);
        productTohandleService.update(productTohandle);
        // 3.保存宠物信息，并且设置成本的价格【员工填写的实际价格】
        Product product = initProduct(orderConfirmDto, orderProduct, productTohandle);
        // 4.更新订单状态和价格，并且加上product的id
        initOrder(orderConfirmDto, orderProduct, product);
    }

    /**
     * 更新订单
     * @param orderConfirmDto
     * @param orderProduct
     * @param product
     */
    private void initOrder(OrderConfirmDto orderConfirmDto, OrderProduct orderProduct, Product product) {
        orderProduct.setPrice(orderConfirmDto.getMoney());
        orderProduct.setState(3);
        orderProductMapper.update(orderProduct);
    }

    /**
     * 初始化服务表
     * @param orderConfirmDto
     * @param orderProduct
     * @return
     */
    private Product initProduct(OrderConfirmDto orderConfirmDto, OrderProduct orderProduct, ProductTohandle productTohandle) {
        Product product = new Product();
        product.setShopId(orderProduct.getShopId());
        productService.add(product);
        return product;
    }

    @Override
    public void cancel(Long orderId) {
        // 1.根据Id查询订单
        OrderProduct orderProduct = orderProductMapper.findById(orderId);
        if (orderProduct == null){
            throw new BussinessException("订单不存在！");
        }
        ProductTohandle productTohandle = productTohandleService.findById(orderProduct.getProductId());
        if (productTohandle == null){
            throw new BussinessException("寻主消息不存在！");
        }
        productTohandle.setState(1);
        productTohandleService.update(productTohandle);
        // 3.修改订单状态为取消
        orderProduct.setState(-1);
        orderProductMapper.update(orderProduct);
        // 4.保存取消日志
        OrderCancleLog orderCancleLog = new OrderCancleLog();
        orderCancleLog.setOrderId(orderId);
        orderCancleLog.setCancelShopid(orderProduct.getShopId());
        orderCancleLogService.add(orderCancleLog);
    }
}
